﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace LocalizationTest.Tools
{
  public partial class ProfileManager : UserControl
  {
    public class StateSavingAttribute : Attribute
    {
      // The constructor is called when the attribute is set.
      public StateSavingAttribute()
      {
      }
    }

    List<string> profiles = new List<string>();

    public ProfileManager()
    {
      InitializeComponent();
    }

    private void buttonSave_Click(object sender, EventArgs e)
    {
      string currentProfileName = comboBoxProfileNames.Text;
      CreateNewProfile(currentProfileName);
    }
    public void ReOpenProfile()
    {
      OpenProfile(comboBoxProfileNames.Text);
    }
    private void ProfileManager_Load(object sender, EventArgs e)
    {
      if (ConfigurationTool.Configuration != null)
      {
        string listOfProfilesAsString = ConfigurationTool.Configuration.ReadConfigurationEntry("profiles", "");
        string[] parts = listOfProfilesAsString.Split(';');
        foreach (string part in parts)
        {
          if (part != "")
          {
            profiles.Add(part);
            comboBoxProfileNames.Items.Add(part);
          }
        }
        string selectedProfile = ConfigurationTool.Configuration.ReadConfigurationEntry("selectedProfile", "");
        comboBoxProfileNames.Text = selectedProfile;
      }
    }
    private void comboBoxProfileNames_SelectedValueChanged(object sender, EventArgs e)
    {
      string selectedProfile = comboBoxProfileNames.Text;
      ConfigurationTool.Configuration.WriteConfigurationEntry("selectedProfile", comboBoxProfileNames.Text);
      OpenProfile(selectedProfile);
    }

    private void CreateNewProfile(string currentProfileName)
    {
      if (!profiles.Contains(currentProfileName))
      {
        profiles.Add(currentProfileName);
        comboBoxProfileNames.Items.Add(currentProfileName);
      }
      string listOfProfilesAsString = "";
      foreach (string profileName in profiles)
      {
        listOfProfilesAsString += profileName + ";";
      }
      ConfigurationTool.Configuration.WriteConfigurationEntry("profiles", listOfProfilesAsString);
      //
      string drawingToolsString = DebugDrawingTool.GetState();
      string debugKeyToolsString = DebugKeyTool.GetState();
      ConfigurationTool.Configuration.WriteConfigurationEntry(
        "profile-" + currentProfileName, 
        drawingToolsString + "^" +
        debugKeyToolsString + "^" + "^");
    }

    private void OpenProfile(string profileName)
    {
      string profileString = ConfigurationTool.Configuration.ReadConfigurationEntry("profile-" + profileName, "");
      string[] parts = profileString.Split('^');
      if(parts.Length > 0) DebugDrawingTool.SetState(parts[0]);
      if (parts.Length > 1) DebugKeyTool.SetState(parts[1]);
    }
  
  }
}
